
package com.action;

import java.io.PrintWriter;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.common.uilt.BaseAction;
import com.google.gson.Gson;
import com.model.MeasureBean;
import com.model.Messages;
import com.model.SuppliersBean;
import com.service.imp.MeasureService;

@Controller
@ParentPackage("defaultpackage")
@Namespace("/back")
@Action(value = "basis", results = {
		@Result(name = "measure_list", location = "/back/basis/template.jsp"),
		@Result(name = "suppliers_list", location = "/back/basis/suppliers.jsp"),
		@Result(name = "error", location = "/hello.jsp"), })
public class BasisAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 678105624309724758L;
	@Autowired
	private MeasureService measureServic;
	private MeasureBean measurebean;
	private String type;
	private SuppliersBean suppliers;
	private String id;
	
	public String basis_list() throws Exception{
		try {
			request.setAttribute("basisList", this.measureServic.list(request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "measure_list";
	}
	public String suppliers_list(){
		try {
			request.setAttribute("basisList", this.measureServic.getSuppliersList());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			
		}
		return "suppliers_list";
	}
	public String addsuppliers() throws Exception{
		Messages m=new Messages();
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		try {
			m=this.measureServic.addsuppliers(this.suppliers);
		} catch (Exception e) {
			e.printStackTrace();
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		out.flush();
		out.close();
		return NONE;
	}
	public String editsuppliers() throws Exception{
		Messages m=new Messages();
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		try {
			m=this.measureServic.editsuppliers(this.suppliers);
		} catch (Exception e) {
			e.printStackTrace();
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		out.flush();
		out.close();
		return NONE;
	}
	public String delsuppliers() throws Exception{
		Messages m=new Messages();
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		try {
			m=this.measureServic.removesuppliers(this.id);
		} catch (Exception e) {
			e.printStackTrace();
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		out.flush();
		out.close();
		return NONE;
	}
	public String getbasis_josn() throws Exception{
		PrintWriter out = response.getWriter();
		try {
			Gson gson = new Gson();
			List<MeasureBean> l=this.measureServic.list(type);
			String jsonStr = gson.toJson(l);
			out.print(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		out.flush();
		out.close();
		return NONE;
	}
	public String deletl() throws Exception{
		Messages m=new Messages();
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		try {
			m=this.measureServic.delete(measurebean.getMeasure_id());
		} catch (Exception e) {
			e.printStackTrace();
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		out.flush();
		out.close();
		return NONE;
	}
	public String add() throws Exception{
		Messages m=new Messages();
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		try {
			m=this.measureServic.add(measurebean);
		} catch (Exception e) {
			e.printStackTrace();
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		out.flush();
		out.close();
		return NONE;
	}
	public String edit() throws Exception{
		Messages m=new Messages();
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		try {
			m=this.measureServic.edit(measurebean);
		} catch (Exception e) {
			e.printStackTrace();
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		out.flush();
		out.close();
		return NONE;
	}
	
	public MeasureBean getMeasurebean() {
		return measurebean;
	}

	public void setMeasurebean(MeasureBean measurebean) {
		this.measurebean = measurebean;
	}

	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
	public SuppliersBean getSuppliers() {
		return suppliers;
	}
	public void setSuppliers(SuppliersBean supliers) {
		this.suppliers = supliers;
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	/**
	 * 计量单位
	 * 
	 * @return
	 */
	public String measure_list() {
		
		return "measure_list";
	}

	/**
	 * 资产来源
	 * 
	 * @return
	 */
	public String source_list() {

		return "measure_list";
	}

	/**
	 * 资产状态
	 * 
	 * @return
	 */
	public String status_list() {

		return "measure_list";
	}

	/**
	 * 减少方式
	 * 
	 * @return
	 */
	public String reduction_mode_list() {

		return "measure_list";
	}

	/**
	 * 仓库
	 * 
	 * @return
	 */
	public String dopot_list() {

		return "measure_list";
	}

	/**
	 * 供货商
	 * 
	 * @return
	 */
	public String supplier_list() {

		return "measure_list";
	}

	/**
	 * 员工职位
	 * 
	 * @return
	 */
	public String office_list() {

		return "measure_list";
	}

}